package com.specter.mvc.web.spring;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

public class ConventionRequestMappingNamingStrategy implements HandlerMethodMappingNamingStrategy<RequestMappingInfo> {

	/**
	 * Separator between the type and method-level parts of a HandlerMethod
	 * mapping name
	 */
	public static final String SEPARATOR = "#";

	@Override
	public String getName(HandlerMethod handlerMethod, RequestMappingInfo mapping) {
		if (mapping.getName() != null) {
			return mapping.getName();
		}
		StringBuilder sb = new StringBuilder();
		String simpleTypeName = handlerMethod.getBeanType().getSimpleName();
		for (int i = 0; i < simpleTypeName.length(); i++) {
			if (Character.isUpperCase(simpleTypeName.charAt(i))) {
				sb.append(simpleTypeName.charAt(i));
			}
		}
		sb.append(SEPARATOR).append(handlerMethod.getMethod().getName());
		
		// System.out.println("namingStrategy method:" + handlerMethod.toString());
		// System.out.println("namingStrategy mapping:" + mapping.toString());
		// System.out.println("namingStrategy:" + sb.toString());

		return sb.toString();
	}

}
